package com.hsq.androidproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.db.DBHelper;
import com.hsq.androidproject.entity.HotStars;
import com.hsq.androidproject.entity.bd_entity.Follow;
import com.hsq.androidproject.ui.ShowHotStartsActivity;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;

import java.util.List;


/**
 * Created by lenovo on 2016/11/22.
 */

public class MyListViewStarRankingAdapter extends BaseAdapter {        ////明星排行榜
    private List<HotStars.HotStarsBean> mData;
    private Context context;

    private DBHelper dbHelper;          ///////数据库 是否关注
    String usersName = "peng";           ////用来判断是否登录

    public MyListViewStarRankingAdapter(List<HotStars.HotStarsBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_listview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.tv_fans = (TextView) convertView.findViewById(R.id.tv_fans);
            viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_follow = (TextView) convertView.findViewById(R.id.tv_follow);
            viewHolder.ll_layout = (LinearLayout) convertView.findViewById(R.id.ll_layout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final HotStars.HotStarsBean hotStarsBean = mData.get(position);
        final String thumb = hotStarsBean.getThumb();
        Glide.with(context)
                .load(thumb)
                .transform(new GlideCircleTransform(context))//圆形
                .into(viewHolder.iv_img);

        final String name = hotStarsBean.getName();
        viewHolder.tv_name.setText(name);
        int count = hotStarsBean.getRank();
        viewHolder.tv_count.setText(count + "");
        int fans = hotStarsBean.getFansCount();
        viewHolder.tv_fans.setText("粉丝数：" + fans);

        dbHelper = new DBHelper(context);
        final Follow follow = new Follow(viewHolder.tv_name.getText().toString(), usersName);
        if (dbHelper.isExistF(follow)) {             //////已经关注
            viewHolder.tv_follow.setText("已关注");
        } else {                                        ///////没有关注
            viewHolder.tv_follow.setText("+关注");
        }
        viewHolder.tv_follow.setOnClickListener(new View.OnClickListener() {        ////加入数据库 是否关注
            @Override
            public void onClick(View v) {
              if (dbHelper.isExistF(follow)) {             //////已经关注
                    dbHelper.deleteFollowByNameAndUserName(follow);
                    viewHolder.tv_follow.setText("+关注");
                } else {                                        ///////没有关注
                    Follow collect = new Follow(hotStarsBean.getName(), usersName, hotStarsBean.getBaikeUrl(), hotStarsBean.getThumb(), hotStarsBean.getSid(), hotStarsBean.getRank(), hotStarsBean.getFansCount());
                    dbHelper.insert(collect);
                    viewHolder.tv_follow.setText("已关注");
                }
            }
        });

        viewHolder.ll_layout.setOnClickListener(new View.OnClickListener() {        ///点击跳转页面
            @Override
            public void onClick(View v) {
                String name = hotStarsBean.getName();
                String baileUrl = hotStarsBean.getBaikeUrl();
                String thumb = hotStarsBean.getThumb();
                int sid = hotStarsBean.getSid();
                int rank = hotStarsBean.getRank();
                int fansCount = hotStarsBean.getFansCount();
                Intent intent = new Intent(context, ShowHotStartsActivity.class);        ////展示每个明星的信息
                intent.putExtra("name", name);
                intent.putExtra("baileUrl", baileUrl);
                intent.putExtra("thumb", thumb);
                intent.putExtra("sid", sid);
                intent.putExtra("rank", rank);
                intent.putExtra("fansCount", fansCount);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder {
        public ImageView iv_img;        //头像
        private TextView tv_fans, tv_count, tv_name, tv_follow; ////粉丝 count排名 姓名 关注
        private LinearLayout ll_layout;
    }
}
